package adt;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;

import kernel.CPU;

/**
 * This class simulates a PCB. The PCB has all the information
 * necessary to describe a process.
 * 
 * @author Aaron Fung
 *		   Chris Ren
 * @version 1.0	
 */
public class ProcessControlBlock {
	
	/**
	 * step count.
	 */
	private int my_step;
	
	/**
	 * the total number of event's is going generate in one run before reset count.
	 */
	private static final int NUM_OF_EVENTS = 10;
	
	/**
	 * the event's occur step list.
	 */
	private ArrayList<Integer> my_events = new ArrayList<Integer>();
	
	/**
	 * the next event step.
	 */
	private int my_next_event = 0;
	
	/**
	 * constructor that sets the step to 0. and randomly generate where the sevice going to occur. 
	 */
	public ProcessControlBlock()
	{
		my_step = 0;
		Random int_gen = new Random();
		for (int i = 0; i < NUM_OF_EVENTS; i++)
		{
			my_events.add(int_gen.nextInt(CPU.RESETCOUNT - CPU.RESETCOUNT/10) + (CPU.RESETCOUNT/10));
		}
		Collections.sort(my_events);
	}
	
	/**
	 * return the step count.
	 */
    public int getStep() 
    {
    	return my_step;
    }
    
    /**
     * reset the step count to 0.
     */
    public void resetStep()
    {
    	my_step = 0;
    	my_next_event = 0;
    }
    
    /**
     * get the next step.
     * @return the next event step count.
     */
    public int getEvent()
    {
    	if (my_next_event < (my_events.size() - 1))
    		return my_events.get(my_next_event);
    	else
    		return CPU.RESETCOUNT;
    }
    
    /**
     * called by CPU, if service occured set the next event step to the next one in queue.
     */
    public void service_occur()
    {
    	if (my_next_event < my_events.size() - 1)
    		my_next_event++;
    }
    
    /**
     * increase step count.
     */
    public void stepIncrease()
    {
    	my_step ++;
    }
}
